//页面布局相关全局通用方法和数据
import defaultSettings from '@/settings'

const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings

const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || ''

const state = {
  //网站标题
  title: '',
  //主题颜色
  theme: storageSetting.theme || '#409EFF',
  //侧边栏主题
  sideTheme: storageSetting.sideTheme || sideTheme,
  //是否系统布局配置
  showSettings: showSettings,
  //是否显示顶部导航
  topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
  //是否显示标签页
  tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
  //是否固定头部
  fixedHeader: storageSetting.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
  //是否显示logo
  sidebarLogo: storageSetting.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
  //是否显示动态标题
  dynamicTitle: storageSetting.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle
}

//mutations是Vuex中唯一允许更新应用状态的函数，是同步函数
const mutations = {
  /**
   * 修改布局设置
   * @param state
   * @param key
   * @param value
   * @constructor
   */
  CHANGE_SETTING: (state, { key, value }) => {
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
  }
}

//actions是Vuex中处理异步操作的函数，它们不能直接修改状态，而是通过调用 mutations 来间接修改状态
const actions = {
  /**
   * 修改布局设置
   * @param commit
   * @param data
   */
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  },
  /**
   * 设置网页标题
   * @param commit
   * @param title
   */
  setTitle({ commit }, title) {
    state.title = title
  }
}

export default {
  // 必须加模块名前缀
  namespaced: true,
  state,
  mutations,
  actions
}

